package com.whisper.application.app.web.controller.front;

import com.whisper.application.service.domain.VrAct;
import com.whisper.application.service.domain.VrApply;
import com.whisper.application.service.service.IVrActService;
import com.whisper.application.service.service.IVrApplyService;
import com.whisper.common.core.controller.BaseController;
import com.whisper.common.exception.operate.OperateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户活动申请Controller
 * 
 * @author whisper
 * @date 2024-01-07
 */
@RestController
@RequestMapping("/front/apply")
public class FrontApplyController extends BaseController
{
    @Autowired
    private IVrApplyService vrApplyService;

    @Autowired
    private IVrActService vrActService;

    /**
     * 获取用户活动申请详细信息
     */
    @GetMapping(value = "/{id}")
    public VrApply getInfo(@PathVariable("id") Long id)
    {
        return vrApplyService.selectVrApplyById(id);
    }

    /**
     * 获取用户活动申请详细信息
     */
    @GetMapping(value = "/act/{id}")
    public int getActApplyCount(@PathVariable("id") Long id)
    {
        return vrApplyService.selectActApplyCount(id);
    }

    /**
     * 新增用户活动申请
     */
    @PostMapping
    public void add(@RequestBody VrApply vrApply)
    {
        VrAct vrAct = vrActService.selectVrActById(vrApply.getActId());
        Integer capacity = vrAct.getCapacity();
        VrApply apply = new VrApply();
        apply.setActId(vrApply.getActId());
        List<VrApply> applies = vrApplyService.selectVrApplyList(vrApply);
        if (!applies.isEmpty()) {
            throw new OperateException("此活动已申请过！");
        }
        List<VrApply> vrApplies = vrApplyService.selectVrApplyList(apply);
        if (vrApplies.size() >= capacity) {
            throw new OperateException("该活动已报满人数！");
        }
        vrApplyService.insertVrApply(vrApply);
    }

    /**
     * 删除用户活动申请
     */
	@DeleteMapping("/{ids}")
    public void remove(@PathVariable Long[] ids)
    {
        vrApplyService.deleteVrApplyByIds(ids);
    }
}
